package com.songlanyun.modules.marketing.seckill.service.impl;

import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.songlanyun.common.enums.MarketingConstant;
import com.songlanyun.common.exception.RRException;
import com.songlanyun.common.utils.DateUtil;
import com.songlanyun.common.utils.DateUtils;
import com.songlanyun.common.utils.custom.DateCustomUtils;
import com.songlanyun.modules.exception.MarketingException;
import com.songlanyun.modules.marketing.seckill.dao.SeckillActivityTimeItemDao;
import com.songlanyun.modules.marketing.seckill.entity.SeckillActivity;
import com.songlanyun.modules.marketing.seckill.entity.SeckillActivityTimeItem;
import com.songlanyun.modules.marketing.seckill.model.dto.SeckillActivityDTO;
import com.songlanyun.modules.marketing.seckill.service.SeckillActivityTimeItemService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.Query;


@Service("marketingSeckillActivityTimeItemService")
public class SeckillActivityTimeItemServiceImpl extends ServiceImpl<SeckillActivityTimeItemDao, SeckillActivityTimeItem> implements SeckillActivityTimeItemService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SeckillActivityTimeItem> page = this.page(
                new Query<SeckillActivityTimeItem>().getPage(params),
                new QueryWrapper<SeckillActivityTimeItem>()
        );

        return new PageUtils(page);
    }

    /**
     * 初始值
     *
     * @param seckillActivity
     */
    @Override
    public SeckillActivityTimeItem setTimeItem(SeckillActivity seckillActivity, Date day, int weekOfDate) {
        SeckillActivityTimeItem timeItem = new SeckillActivityTimeItem();

        timeItem.setSeckillId(seckillActivity.getId());
        timeItem.setShopId(seckillActivity.getShopId());
        timeItem.setDay(day);//日期
        //开始时间（小时）
        String startHour = seckillActivity.getStartHour();
        timeItem.setStartHour(startHour);
        //结束时间（小时）
        String endHour = seckillActivity.getEndHour();
        timeItem.setEndHour(endHour);

        String dayStr = DateCustomUtils.dateToString(day);
        //活动开始时间 日期+开始时间（小时）
        String startStr = dayStr + " " + startHour;
        Date startTime = DateUtils.stringToDate(startStr);
        timeItem.setStartTime(startTime);
        //活动结束时间 日期+结束时间（小时）
        String endStr = dayStr + " " + endHour;
        Date endTime = DateUtils.stringToDate(endStr);
        timeItem.setEndTime(endTime);

        timeItem.setWeek(MarketingConstant.Week.getByCode(weekOfDate));//周几

        return timeItem;
    }

    /**
     * 校验该商家在日期时间内有没有创建过活动，创建过则不能在创建
     */
    @Override
    public void verifySeckillActivity(SeckillActivityTimeItem timeItem) {
        int count = this.baseMapper.selectCount(
                new LambdaQueryWrapper<SeckillActivityTimeItem>()
                        .eq(SeckillActivityTimeItem::getShopId, timeItem.getShopId())
                        .eq(SeckillActivityTimeItem::getDay, timeItem.getDay())
                        .eq(SeckillActivityTimeItem::getStartHour, timeItem.getStartHour())
        );
        if (count > 0) {
            throw new RRException(MarketingException.SECKILL_ACTIVITY_TIME_USEING);
        }

    }

    @Override
    public List<SeckillActivityTimeItem> getListBySeckillId(Long id) {
        return this.list(
                new LambdaQueryWrapper<SeckillActivityTimeItem>()
                        .eq(SeckillActivityTimeItem::getSeckillId, id)
        );

    }

    /**
     * 删除秒杀活动的日期记录
     *
     * @param seckillId
     * @return
     */
    @Override
    public boolean removeBySeckillId(Long seckillId) {

        return this.remove(
                new LambdaQueryWrapper<SeckillActivityTimeItem>()
                        .eq(SeckillActivityTimeItem::getSeckillId, seckillId)
        );

    }

    @Override
    public SeckillActivityTimeItem getNowOne(Map<String, Object> params) {
        Long shopId = MapUtil.getLong(params, "shop_id");
        Date date = new Date();
        return this.baseMapper.selectOne(
                new LambdaQueryWrapper<SeckillActivityTimeItem>()
                        .eq(shopId != null, SeckillActivityTimeItem::getShopId, shopId)
                        .le(SeckillActivityTimeItem::getStartTime, date)
                        .gt(SeckillActivityTimeItem::getEndTime, date)
                        .last("limit 1")
        );
    }

    @Override
    public SeckillActivityTimeItem currentTimeOneSeckill(String startTime, Long shopId) {
        return this.baseMapper.selectOne(
                new LambdaQueryWrapper<SeckillActivityTimeItem>()
                        .eq(shopId != null, SeckillActivityTimeItem::getShopId, shopId)
                        .eq(SeckillActivityTimeItem::getStartTime, startTime)
                        .last("limit 1")
        );
    }

}
